home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Dialectic code / dialectic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  5.1 KB  |  220 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        dialectic.c
  4.  
  5. Purpose:    This module handles Dialectic-specific initialization and
  6.             shutdown, memory setup for converting, and a dispatch
  7.             for file conversion.
  8.  
  9.  
  10. Dialectic -=- dialect text conversion extraordinare
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "graphics.h"
  31. #include "dialectic.h"
  32. #include "dialectic dispatch.h"
  33. #include "dialectic graphics.h"
  34. #include "file interface.h"
  35. #include "dialectic file management.h"
  36. #include "error.h"
  37. #include "progress.h"
  38. #include "program globals.h"
  39. #include "util.h"
  40. #include "environment.h"
  41. #include "menus.h"
  42.  
  43. unsigned char    gShowSaveDialog;
  44. unsigned char    gAddSuffix;
  45. unsigned char    gShowProgress;
  46. unsigned char    gUseRTF;
  47.  
  48. void InitTheProgram(void)
  49. {
  50.     int                i,j,k;
  51.     AppFile            myFile;
  52.     
  53.     (**(gTheWindowData[kClipboard])).dispatchProc=ClipboardWindowDispatch;
  54.     ((**(gTheWindowData[kClipboard])).dispatchProc)(gTheWindowData[kClipboard], kStartup, 0L);
  55.     
  56.     CountAppFiles(&i, &j);
  57.     if ((j>0) && (i==0))
  58.     {
  59.         for (k=1; k<=j; k++)
  60.         {
  61.             GetAppFiles(1, &myFile);
  62.             MyMakeFSSpec(myFile.vRefNum, 0, myFile.fName, &inputFS);
  63.             StartConvert();
  64.             ClrAppFiles(k);
  65.         }
  66.     }
  67. }
  68.  
  69. void NewConvert(void)
  70. {
  71.     inputFS.name[0]=outputFS.name[0]=tempFS.name[0]=0x00;
  72.     if (GetSourceFile(&inputFS))
  73.         StartConvert();
  74. }
  75.  
  76. void StartConvert(void)
  77. {
  78.     int                err;
  79.     Boolean            notDoneYet;
  80.     Str255            titleStr;
  81.     
  82.     outputFS=inputFS;
  83.     if (gAddSuffix)
  84.         SetupSuffix();
  85.     deleteTheThing=TRUE;
  86.     
  87.     if (gShowSaveDialog)
  88.         if (!GetDestFile(&outputFS, &deleteTheThing, "\pSave converted text as..."))
  89.             return;
  90.  
  91.     gInputNeedsUpdate=TRUE;
  92.     gOutputNeedsUpdate=FALSE;
  93.     gInputOffset=gOutputOffset=gAbsoluteOffset=0L;
  94.     
  95.     InitFiles();
  96.     HandleError(err=OpenInputFile(), FALSE);
  97.     if (err!=allsWell)
  98.         return;
  99.     HandleError(err=CreateTempFile(), FALSE);
  100.     if (err!=allsWell)
  101.     {
  102.         FinalizeFiles(FALSE);
  103.         return;
  104.     }
  105.     HandleError(err=SetupTempFile(), FALSE);
  106.     if (err!=allsWell)
  107.     {
  108.         FinalizeFiles(FALSE);
  109.         return;
  110.     }
  111.     
  112.     gInputBuffer=NewPtrClear(INPUT_BUFFER_MAX+256);
  113.     if (gInputBuffer==0L)
  114.     {
  115.         FinalizeFiles(FALSE);
  116.         HandleError(kNoMemory, FALSE);
  117.         return;
  118.     }
  119.     
  120.     gOutputBuffer=NewPtrClear(OUTPUT_BUFFER_MAX+256);
  121.     if (gOutputBuffer==0L)
  122.     {
  123.         FinalizeFiles(FALSE);
  124.         DisposePtr(gInputBuffer);
  125.         HandleError(kNoMemory, FALSE);
  126.         return;
  127.     }
  128.     
  129.     if ((!gShowProgress) || (gInputLength<=INPUT_BUFFER_MAX))
  130.     {
  131.         gInProgress=TRUE;
  132.         AdjustMenus();
  133.         DrawMenuBar();
  134.     }
  135.     else
  136.     {
  137.         GetItem(gDialectMenu, gWhichDialect+1, titleStr);
  138.         OpenProgressDialog(gInputLength, titleStr);
  139.         SetProgressText("\pConverting ",inputFS.name, "\p...","\p");
  140.     }
  141.     
  142.     gDoingRTF=gInWord=gSeenI=gSeenBackslash=FALSE;
  143.     gCurlyLevel=0;
  144.     
  145.     notDoneYet=TRUE;
  146.     err=allsWell;
  147.     while ((notDoneYet) && (err==allsWell) && (gAbsoluteOffset<gInputLength))
  148.     {
  149.         if (gInputNeedsUpdate)
  150.         {
  151.             if (gInputOffset>0L)
  152.                 ShiftInputBuffer();
  153.             err=ReadInputFile(inputRefNum, gInputBuffer+gWhatsReallyInInputBuffer,
  154.                     INPUT_BUFFER_MAX+256-gWhatsReallyInInputBuffer);
  155.             if (err!=allsWell)
  156.             {
  157.                 DismissProgressDialog();
  158.                 HandleError(err, FALSE);
  159.             }
  160.             
  161.             gInputNeedsUpdate=FALSE;
  162.             if ((gShowProgress) && (gInputLength>INPUT_BUFFER_MAX) && (gInProgress))
  163.             {
  164.                 UpdateProgressDialog(gAbsoluteOffset);
  165.                 notDoneYet=DealWithOtherPeople();
  166.             }
  167.         }
  168.         
  169.         if ((notDoneYet) && (err==allsWell) && (gOutputNeedsUpdate))
  170.         {
  171.             err=WriteTempFile(outputRefNum, gOutputBuffer, gOutputOffset);
  172.             if (err!=allsWell)
  173.             {
  174.                 DismissProgressDialog();
  175.                 HandleError(err, FALSE);
  176.             }
  177.             gOutputOffset=0L;
  178.             gOutputNeedsUpdate=FALSE;
  179.         }
  180.         
  181.         if ((notDoneYet) && (err==allsWell))
  182.             ConvertDispatch();
  183.     }
  184.     
  185.     if ((gShowProgress) && (gInputLength>INPUT_BUFFER_MAX) && (gInProgress))
  186.     {
  187.         UpdateProgressDialog(gAbsoluteOffset);
  188.         DealWithOtherPeople();
  189.     }
  190.     
  191.     if ((gOutputOffset>0L) && (err==allsWell))
  192.     {
  193.         err=WriteTempFile(outputRefNum, gOutputBuffer, gOutputOffset);
  194.         if (err!=allsWell)
  195.         {
  196.             DismissProgressDialog();
  197.             HandleError(err, FALSE);
  198.         }
  199.     }
  200.     
  201.     DisposePtr(gInputBuffer);
  202.     DisposePtr(gOutputBuffer);
  203.     
  204.     FinalizeFiles((notDoneYet) && (err==allsWell));
  205.     
  206.     DismissProgressDialog();
  207. }
  208.  
  209. void ShiftInputBuffer(void)
  210. {
  211.     Mymemcpy(gInputBuffer, gInputBuffer+gInputOffset, INPUT_BUFFER_MAX+256-gInputOffset);
  212.     gWhatsReallyInInputBuffer=INPUT_BUFFER_MAX+256-gInputOffset;
  213.     gInputOffset=0L;
  214. }
  215.  
  216. void ShutDownTheProgram(void)
  217. {
  218.     ((**(gTheWindowData[kClipboard])).dispatchProc)(gTheWindowData[kClipboard], kShutdown, 0L);
  219. }
  220.